//
//  MainWindowController.swift
//  Todo
//
//  Created by reylen on 16/7/6.
//  Copyright © 2016年 reylen. All rights reserved.
//

import Cocoa

class MainWindowController: NSWindowController,NSTableViewDataSource,NSTableViewDelegate {

    @IBOutlet weak var textField: NSTextField!
    
    @IBOutlet weak var tableView: NSTableView!
    
    var dataSource:[String] = []
    
    
    override var windowNibName: String? {
        return "MainWindowController"
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
        self.tableView.headerView = nil
    }
    
    // MARK: - Action
    @IBAction func add(sender: AnyObject) {
        let valule = textField.stringValue
        if !valule.isEmpty {
            dataSource.append(valule)
            let indexSet = NSIndexSet.init(index: dataSource.count-1)
            self.tableView.insertRowsAtIndexes(indexSet, withAnimation: NSTableViewAnimationOptions.EffectFade)
            self.tableView.scrollRowToVisible(dataSource.count-1)
            textField.stringValue = ""
        }
    }
    
    // MARK: - TableView DataSource
    
    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return dataSource.count
    }
    
    func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
        return dataSource[row]
    }
    /*
    func tableView(tableView: NSTableView, shouldEditTableColumn tableColumn: NSTableColumn?, row: Int) -> Bool {
        return true
    }
    */
    
}
